package com.lxj.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lxj.common.CodeMsg;
import com.lxj.common.Result;
import com.lxj.domain.BusCar;
import com.lxj.domain.BusRent;
import com.lxj.domain.BusReturn;
import com.lxj.dto.BusReturnDto;
import com.lxj.mapper.BusCarMapper;
import com.lxj.mapper.BusRentMapper;
import com.lxj.mapper.BusReturnMapper;
import com.lxj.service.ReturnService;
import com.lxj.vo.BusReturnVo;
import com.lxj.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author lxj
 * @date 2021/6/21 19:45
 * @Description:
 */
@Service
public class ReturnServiceImpl implements ReturnService {

    @Autowired
    private BusReturnMapper returnMapper;

    @Autowired
    private BusRentMapper rentMapper;

    @Autowired
    private BusCarMapper carMapper;

    /**
     * 归还车辆
     * @param rentId
     * @return
     */
    @Override
    @Transactional
    public Result returnCar(Integer rentId) {
        //通过租车的id 找到租车的人 ,出租的车辆
        BusRent busRent = rentMapper.selectByPrimaryKey(rentId);
        //数据校验
        if (busRent == null) {
            return new Result(CodeMsg.RENT_RECORD_ERROR);
        }
        if(busRent.getFlag() == 1){
            return new Result(CodeMsg.RENT_FLAG_IS_RETURN);
        }
        //将出租记录里面的flag 修改为已经归还
        modifyCarRentStatus(busRent);
        //将出租的车辆状态修改为已经归还
        modifyCarStatus(busRent.getNum());
        //新增一条归还记录
        saveReturnCar(busRent);
        return new Result();
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageInfo<BusReturnVo> findByPage(BusReturnDto dto) {
        Page<BusReturnVo> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<BusReturnVo> busReturns = returnMapper.selectAll(dto);
        return new PageInfo<BusReturnVo>(page.getTotal(),busReturns);
    }

    /**
     * 新增一条归还记录
     * @param busRent
     */
    private void saveReturnCar(BusRent busRent) {
        BusReturn busReturn = new BusReturn();
        busReturn.setNum(busRent.getNum());
        Integer rentPrice = busRent.getRentPrice();
        //将租车的总时长转化为小时为单位
        Long totalTime = DateUtil.between(DateUtil.parse(busRent.getEndTime(), "yyyy-MM-dd"), DateTime.now(), DateUnit.HOUR);
        // 计算总金额 = 每小时的租金 * 租车的总小时
        busReturn.setTotalMoney((int) (rentPrice * totalTime));
        busReturn.setPayMoney(0);
        busReturn.setProblem("");
        busReturn.setUserId(busRent.getUserId());
        busReturn.setReturnTime(DateUtil.formatTime(new Date()));
        busReturn.setRentPrice(busRent.getRentPrice());
        busReturn.setRentId(busRent.getId());
        returnMapper.insertSelective(busReturn);
    }

    /**
     * 修改车辆表的车辆状态为已归还
     * @param num
     */
    private void modifyCarStatus(String num) {
        BusCar busCar = carMapper.selectByNum(num);
        busCar.setIsRent(0);
        carMapper.updateByPrimaryKeySelective(busCar);
    }

    /**
     * 修改租车表的借车记录为已归还
     * @param busRent
     */
    private void modifyCarRentStatus(BusRent busRent) {
        busRent.setFlag(1);
        rentMapper.updateByPrimaryKeySelective(busRent);
    }
}
